Windows Auto-Proxy
*
*
*
Hi,
I have been discussing this a bit with Samuli, to see if it makes sense – and it seems that perhaps it does, so let me raise this with the development folks to get your thoughts. Hopefully this is something that would help OpenVPN (or at least that’s my intention!).
I use OpenVPN with a Windows client, and I tend to be on one network one minute, another the next. One time with a proxy, the next time without … so I really want OpenVPN to automatically detect the proxy (if there is one), and apply it … for every connection restart, client open, etc. My thinking is to add a new option, say something like “auto-proxy” (so this won’t break anything that is already working!). The idea being that if this option is enabled, then on every connection / reconnection attempt OpenVPN will first check the proxy, and then apply it for the actual connection back to the server. Hopefully this makes sense so far … J.
To try to help with this, I wrote a small application that detect the Windows proxy settings (even working with PAC files!), based on WinHTTP. I have attached the code to this email, so you can take a look at comment (BTW, it builds just fine in Visual Studio Express 2010). A few thoughts on this code,
- feel free to try it on various configurations and machines (and provide feedback of course!). However, it may not need to be bulletproof before it's included (but feel free to disagree!), as it’s disabled by just by not using the (new) auto-proxy option.
- I have tried it on Win XP and Win 7, with a proxy PAC file ... both on and off the network (so with and without proxy). It works fine in all these cases (and gets the right answer ... :-)).
- I'm not sure how you use proxy in your code. At least with the PAC file that I go through, I get a response back of the format proxy.domain.net:8080;10.10.10.10:8080. We may need to parse this (again, just not sure how you use it).
- I have not implemented asynchronous processing. It can be done, but not sure it buys much, as you can't proceed until the proxy information is available. I also haven't modified the WinHTTP timeout. Again, this can be done if needed (but the response is typically quite quick).
- the slew of messages that I have put in the code are just FYI for now. These could be split to console output, or logging, or just removed.
Whatever folks prefer.
So - please take a look at let me know. I’m not sure exactly how it would be integrated in to the OpenVPN code, as I don’t have the big picture of that. But I’m sure someone else does, and I can modify this as needed to get to that point.
Thanks,
… Russell
/**********************************************************************************
Utility: AutoProxy - automatically determine Windows Proxy (example for OpenVPN) Author : R. Morris
Vesion : 1.00
Reference Information:
1) C++ Character Constants
http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(L);k(DevLang-%22C%2B%2B%22)&rd=true
2) WinHTTP Functions:
WinHttpOpen Function : http://msdn.microsoft.com/en-us/library/aa384098(v=vs.85).aspx
WinHttpGetProxyForUrl Function : http://msdn.microsoft.com/en-us/library/aa384097(v=vs.85).aspx
2) WinHTTP Structures:WINHTTP_CURRENT_USER_IE_PROXY_CONFIG Structure: http://msdn.microsoft.com/en-us/library/aa384250(VS.85).aspxWINHTTP_AUTOPROXY_OPTIONS Structure : http://msdn.microsoft.com/en-us/library/aa384123(v=vs.85).aspxWINHTTP_PROXY_INFO Structure : http://msdn.microsoft.com/en-us/library/aa383912(v=vs.85).aspx
************************************************************************************/
#include <Windows.h>
#include <winhttp.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
// Include WinHTTP Library
#pragma comment(lib, "winhttp.lib")
void main()
{
LPWSTR lpszURL; // LPWSTR: Input URL (pointer to Unicode string, to search for Proxy)
HINTERNET hSession; // HINTERNET: Session Handle (for HTTP Open)
WINHTTP_CURRENT_USER_IE_PROXY_CONFIG IEProxyConfig; // Structure: IE Proxy Configuration (for current user)
WINHTTP_AUTOPROXY_OPTIONS AutoProxyOptions; // Structure: AutoProxy Options
WINHTTP_PROXY_INFO AutoProxyInfo; // Structure: WinHTTP (Auto)Proxy Information
BOOL bURLResult; // Boolean: URL Result (from Get HTTP Proxy Call)
long errNum; // Long: Error Number (from WinHTTP Call)
LPCWSTR lpszDefaultURL = L"http://www.openvpn.net"; // LPCWSTR: Default URL
// Get input from user for site to check proxy for (as proxy can be site dependent)
lpszURL = (LPWSTR)GlobalAlloc(GMEM_FIXED, 512);
wprintf(L"URL to check Proxy for [%s]: ", lpszDefaultURL);
_getws_s(lpszURL, 256);
printf("\nStarting Proxy processing ...\n");
if (wcslen(lpszURL) == 0) {
wprintf(L" Empty URL, using default entry [%s]\n", lpszDefaultURL);
wcscpy_s(lpszURL, 256, lpszDefaultURL);
}
// Retreive Internet Explorer Proxy Configuration (for Current User)
ZeroMemory(&AutoProxyOptions, sizeof(AutoProxyOptions));
ZeroMemory(&AutoProxyInfo, sizeof(AutoProxyInfo));
printf(" Retrieving Internet Explorer Proxy Configuration (for Current User) ...\n");
if (WinHttpGetIEProxyConfigForCurrentUser(&IEProxyConfig) == TRUE) {
// Set Options for Internet Explorer = Automatically Detect Settings (1st Priority)
if (IEProxyConfig.fAutoDetect == TRUE) {
printf(" Internet Explorer calls for AutoDetect\n");
AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;
AutoProxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP | WINHTTP_AUTO_DETECT_TYPE_DNS_A;
AutoProxyOptions.fAutoLogonIfChallenged = TRUE;
// Set Options for Internet Explorer = Use Automatic Configuration Script (2nd Priority)
} else if (IEProxyConfig.lpszAutoConfigUrl != NULL) {
printf(" Internet Explorer calls for Auto Configuration Script\n");
AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL;
AutoProxyOptions.lpszAutoConfigUrl = IEProxyConfig.lpszAutoConfigUrl;
AutoProxyOptions.fAutoLogonIfChallenged = TRUE;
// Set Options for Internet Explorer = Proxy Server (3rd Priority)
} else if (IEProxyConfig.lpszProxy != NULL) {
printf(" Internet Explorer calls for (defined) Proxy Server\n");
AutoProxyInfo.dwAccessType = WINHTTP_ACCESS_TYPE_NAMED_PROXY;
AutoProxyInfo.lpszProxy = IEProxyConfig.lpszProxy;
AutoProxyInfo.lpszProxyBypass = IEProxyConfig.lpszProxyBypass;
// Set Options for Internet Explorer = No Proxy Options Defined (so no proxy!)
} else {
printf(" Internet Explorer does not define any proxy options\n");
AutoProxyInfo.dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
}
} else {
// Retrieving Internet Explorer Configuration failed, so try Auto Detection!
printf(" Internet Explorer Configuration Read Failed (so attempt Auto Detection!)\n");
AutoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;
AutoProxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP | WINHTTP_AUTO_DETECT_TYPE_DNS_A;
}
// Now, retreive Proxy information based on Internet Explorer Configuration (if appropriate)
if (AutoProxyOptions.dwFlags != 0) {
// Need to create a temporary WinHttp session handle -> Note: performance of GetProxyInfoForUrl can be improved by saving this hSession handle across calls instead of creating a new handle each time
printf(" Opening WinHTTP Session\n");
hSession = WinHttpOpen(L"OpenVPN AutoProxy Check", WINHTTP_ACCESS_TYPE_NO_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
// Get Proxy Details for Target URL
printf(" Getting Proxy Details for Target URL ...\n");
bURLResult = WinHttpGetProxyForUrl(hSession, lpszURL, &AutoProxyOptions, &AutoProxyInfo);
// Evaluate Results from URL Specific Proxy check
if (bURLResult == TRUE) {
// Successful, retrieve Proxy details
printf(" Proxy Query Successful, Retrieved Proxy for URL!\n");
} else {
// Proxy retreival failed, so get error number (to determine why it failed) ... and set for no Proxy (as call failed)
printf(" Proxy Query failed, retreive Error Code\n");
errNum = GetLastError();
AutoProxyInfo.dwAccessType = WINHTTP_ACCESS_TYPE_NO_PROXY;
// And display error information (to examine why it failed)
printf(" WinHTTPGetProxyForURL Error: ");
switch (errNum) {
case ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR:
printf("ERROR_WINHTTP_AUTO_PROXY_SERVICE_ERROR\n");
break;
case ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT:
printf("ERROR_WINHTTP_BAD_AUTO_PROXY_SCRIPT\n");
break;
case ERROR_WINHTTP_INCORRECT_HANDLE_TYPE:
printf("ERROR_WINHTTP_INCORRECT_HANDLE_TYPE\n");
break;
case ERROR_WINHTTP_INTERNAL_ERROR:
printf("ERROR_WINHTTP_INTERNAL_ERROR\n");
break;
case ERROR_WINHTTP_INVALID_URL:
printf("ERROR_WINHTTP_INVALID_URL\n");
break;
case ERROR_WINHTTP_LOGIN_FAILURE:
printf("ERROR_WINHTTP_LOGIN_FAILURE\n");
break;
case ERROR_WINHTTP_OPERATION_CANCELLED:
printf("ERROR_WINHTTP_OPERATION_CANCELLED\n");
break;
case ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT:
printf("ERROR_WINHTTP_UNABLE_TO_DOWNLOAD_SCRIPT\n");
break;
case ERROR_WINHTTP_UNRECOGNIZED_SCHEME:
printf("ERROR_WINHTTP_UNRECOGNIZED_SCHEME\n");
break;
case ERROR_NOT_ENOUGH_MEMORY:
printf("ERROR_NOT_ENOUGH_MEMORY\n");
break;
}
}
// Close WinHTTP Session
printf(" Closing WinHTTP Session\n");
WinHttpCloseHandle(hSession);
}
// Print Summary Information (i.e. Proxy details)
printf("\nProxy Details (determined by routine):\n");
printf(" Proxy Type: ");
switch (AutoProxyInfo.dwAccessType) {
case WINHTTP_ACCESS_TYPE_DEFAULT_PROXY:
printf("WINHTTP_ACCESS_TYPE_DEFAULT_PROXY\n");
wprintf(L" Proxy: %s\n", AutoProxyInfo.lpszProxy);
wprintf(L" Proxy Bypass: %s\n", AutoProxyInfo.lpszProxyBypass);
break;
case WINHTTP_ACCESS_TYPE_NO_PROXY:
printf("WINHTTP_ACCESS_TYPE_NO_PROXY\n");
break;
case WINHTTP_ACCESS_TYPE_NAMED_PROXY:
printf("WINHTTP_ACCESS_TYPE_NAMED_PROXY\n");
wprintf(L" Proxy: %s\n", AutoProxyInfo.lpszProxy);
wprintf(L" Proxy Bypass: %s\n", AutoProxyInfo.lpszProxyBypass);
break;
}
// Free up Memory Allocated during the Proxy Detection process
printf("\nProxy Detection completed!\n");
printf("Freeing up Memory Allocated during the Proxy Detection Process ...\n");
if (IEProxyConfig.lpszAutoConfigUrl != NULL) GlobalFree(IEProxyConfig.lpszAutoConfigUrl);
if (IEProxyConfig.lpszProxy != NULL) GlobalFree(IEProxyConfig.lpszProxy);
if (IEProxyConfig.lpszProxyBypass != NULL) GlobalFree(IEProxyConfig.lpszProxyBypass);
if (AutoProxyInfo.lpszProxy != NULL) GlobalFree(AutoProxyInfo.lpszProxy);
if (AutoProxyInfo.lpszProxyBypass != NULL) GlobalFree(AutoProxyInfo.lpszProxyBypass);
GlobalFree(lpszURL);
printf("Memory deallocation processing completed\n\n");
// And exit (letting the user see the info first)
printf ("\nPress any key to exit ...");
_getch();
}Quoted 12 lines · Russell Morris
I use OpenVPN with a Windows client, and I tend to be on one network one minute, another the next. One time with a proxy, the next time without ... so I really want OpenVPN to automatically detect the proxy (if there is one), and apply it ... for every connection restart, client open, etc. My thinking is to add a new option, say something like "auto-proxy" (so this won't break anything that is already working!). The idea being that if this option is enabled, then on every connection / reconnection attempt OpenVPN will first check the proxy, and then apply it for the actual connection back to the server. Hopefully this makes sense so far ... J.
...isn't that already done by "--auto-proxy"? Been part of openvpn since 2.1(ish?)
BTW: I totally agree this is a big deal. For openvpn to be truly brilliant, it needs
1. one config to handle both udp and tcp-based "<connection>" profiles [sorta supported]
2. "fragment", "mss-fix" and proxy support within profiles [not currently supported - which effectively makes "1." never work in practice]
3. dynamically figure out if a proxy is available and use that for TCP-based profiles [I thought that was supported by "--auto-proxy"]
With such features and a properly ordered config, you'd have a VPN client that would tunnel out over UDP if it can, TCP if it can't, and TCP-via-proxy if it has to. Basically, you'd be guaranteed a working VPN session on any network that you're meant to be able to do such things on (with one config).
Signature · 6 lines
Cheers Jason Haar Information Security Manager, Trimble Navigation Ltd. Phone: +64 3 9635 377 Fax: +64 3 9635 417 PGP Fingerprint: 7A2E 0407 C9A6 CAF6 2B9F 8422 C063 5EBB FE1D 66D1
Hi,
I admit, I didn't know about that option - guess I missed that ... :-(.
When you mentioned it though, I was very happy to see it - so I decided to give it a try. It doesn't seem to work for me though (perhaps because of proxy PAC files?). Is there a way to see what proxy OpenVPN detected (to try to debug this)?
Thanks,
... Russell
Quoted 1 line · Jason Haar
Quoted 4 lines · Russell Morris
*
*
*
I use OpenVPN with a Windows client, and I tend to be on one network one minute, another the next. One time with a proxy, the next time without … so I really want OpenVPN to automatically detect the proxy (if there is one), and apply it … for every connection restart, client open, etc. My thinking is to add a new option, say something like “auto-proxy” (so this won’t break anything that is already working!). The idea being that if this option is enabled, then on every connection / reconnection attempt OpenVPN will first check the proxy, and then apply it for the actual connection back to the server. Hopefully this makes sense so far … J.
...isn't that already done by "--auto-proxy"? Been part of openvpn since 2.1(ish?)
BTW: I totally agree this is a big deal. For openvpn to be truly brilliant, it needs
1. one config to handle both udp and tcp-based "<connection>" profiles [sorta supported]
2. "fragment", "mss-fix" and proxy support within profiles [not currently supported - which effectively makes "1." never work in practice]
3. dynamically figure out if a proxy is available and use that for TCP-based profiles [I thought that was supported by "--auto-proxy"]
With such features and a properly ordered config, you'd have a VPN client that would tunnel out over UDP if it can, TCP if it can't, and TCP-via-proxy if it has to. Basically, you'd be guaranteed a working VPN session on any network that you're meant to be able to do such things on (with one config).
Signature · 16 lines
Cheers Jason Haar Information Security Manager, Trimble Navigation Ltd. Phone: +64 3 9635 377 Fax: +64 3 9635 417 PGP Fingerprint: 7A2E 0407 C9A6 CAF6 2B9F 8422 C063 5EBB FE1D 66D1 ------------------------------------------------------------------------------ Magic Quadrant for Content-Aware Data Loss Prevention Research study explores the data loss prevention market. Includes in-depth analysis on the changes within the DLP market, and the criteria used to evaluate the strengths and weaknesses of these DLP solutions. http://www.accelacomm.com/jaw/sfnl/114/51385063/ _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Hi,
Has anyone had any luck with the auto-proxy function, in particular with PAC files? It really doesn't seem to be working for me ... :-(.
Also, does anyone have a proxy server that requires authentication? I currently use a text file (which does work!), but there are also Windows functions that should send domain login information automatically. It would be nice to to that ... :-).
Thoughts?
Thanks!
... Russell
Quoted 1 line · Russell Morris
Hi,
I admit, I didn't know about that option - guess I missed that ... :-(.
When you mentioned it though, I was very happy to see it - so I decided to give it a try. It doesn't seem to work for me though (perhaps because of proxy PAC files?). Is there a way to see what proxy OpenVPN detected (to try to debug this)?
Thanks,
... Russell
Quoted 5 lines · Jason Haar
Quoted 4 lines · Russell Morris
*
*
*
I use OpenVPN with a Windows client, and I tend to be on one network one minute, another the next. One time with a proxy, the next time without … so I really want OpenVPN to automatically detect the proxy (if there is one), and apply it … for every connection restart, client open, etc. My thinking is to add a new option, say something like “auto-proxy” (so this won’t break anything that is already working!). The idea being that if this option is enabled, then on every connection / reconnection attempt OpenVPN will first check the proxy, and then apply it for the actual connection back to the server. Hopefully this makes sense so far … J.
...isn't that already done by "--auto-proxy"? Been part of openvpn since 2.1(ish?)
BTW: I totally agree this is a big deal. For openvpn to be truly brilliant, it needs
1. one config to handle both udp and tcp-based "<connection>" profiles [sorta supported]
2. "fragment", "mss-fix" and proxy support within profiles [not currently supported - which effectively makes "1." never work in practice]
3. dynamically figure out if a proxy is available and use that for TCP-based profiles [I thought that was supported by "--auto-proxy"]
With such features and a properly ordered config, you'd have a VPN client that would tunnel out over UDP if it can, TCP if it can't, and TCP-via-proxy if it has to. Basically, you'd be guaranteed a working VPN session on any network that you're meant to be able to do such things on (with one config).
Signature · 24 lines
Cheers Jason Haar Information Security Manager, Trimble Navigation Ltd. Phone: +64 3 9635 377 Fax: +64 3 9635 417 PGP Fingerprint: 7A2E 0407 C9A6 CAF6 2B9F 8422 C063 5EBB FE1D 66D1 ------------------------------------------------------------------------------ Magic Quadrant for Content-Aware Data Loss Prevention Research study explores the data loss prevention market. Includes in-depth analysis on the changes within the DLP market, and the criteria used to evaluate the strengths and weaknesses of these DLP solutions. http://www.accelacomm.com/jaw/sfnl/114/51385063/ _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel ------------------------------------------------------------------------------ Got Input? Slashdot Needs You. Take our quick survey online. Come on, we don't ask for help often. Plus, you'll get a chance to win $100 to spend on ThinkGeek. http://p.sf.net/sfu/slashdot-survey _______________________________________________ Openvpn-devel mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/openvpn-devel
Hi,
Quoted 2 lines · On Monday 08 August 2011 01:15:02 Russell Morris wrote:
Has anyone had any luck with the auto-proxy function, in particular with PAC files? It really doesn't seem to be working for me ... :-(.
Are you aware of https://community.openvpn.net/openvpn/ticket/24 ? This is probably what's causing your trouble... and of course the fact that the current impelmentation does not do proxy auto detection.
I'll start resolving these issues now as it is something the GUI is relying onto as well. Openvpn should even be able to handle per Dial-up and VPN connection proxy settings if things work out.
Quoted 4 lines
Also, does anyone have a proxy server that requires authentication? I currently use a text file (which does work!), but there are also Windows functions that should send domain login information automatically. It would be nice to to that ... :-).
I like that idea, but t's probably unrelated to auto-proxy. Could you point me to the APIs.
Heiko
Signature · 8 lines
Heiko Hund | Software Engineer | Phone +49-721-25516-237 | Fax -200 Astaro a Sophos Company | Amalienbadstr. 41 Bau 52 | 76227 Karlsruhe | Germany Commercial Register: Mannheim HRA 702710 | Headquarter Location: Karlsruhe Represented by the General Partner Astaro Verwaltungs GmbH Amalienbadstraße 41 Bau 52 | 76227 Karlsruhe | Germany Commercial Register: Mannheim HRB 708248 | Executive Board: Gert Hansen, Markus Hennig, Jan Hichert, Günter Junk, Dr. Frank Nellissen
Hi Heiko,
Well, I can try ... :-). Some thoughts here - hope this helps, but of course feel free to ask questions!
1) I have attached some code I put together to detect proxy information automatically (using WinHTTP, and in particular WinHttpGetProxyForUrl). It works with PAC files (at least it did for me ... :-)). I have also included some links in the code, to get more info on the Windows API's. Please excuse the poor coding though - I'm a HW guy, not a SW expert (not even close!). The slew of messages that I have put in the code are just FYI for now. These could be split to console output, or logging, or just removed. Whatever you prefer. BTW, I'm not sure how you use proxy in your code. At least with the PAC file that I go through, I get a response back of the format proxy.domain.net:8080;10.10.10.10:8080. We may need to parse this (again, just not sure how you use it). Also, I have not implemented asynchronous processing. It can be done, but not sure it buys much, as you really can't proceed until the proxy information is available. I also haven't modified the WinHTTP timeout. Again, this can be done if needed (but the response is typically quite quick). I agree with your targets - and one further comment ... auto-proxy detection should be done every time a connection attempt is to be started (i.e. even when reconnecting), agreed?
2) I can't say too much about automatic logon, but I have seen some example code on Microsoft's web site related to using fAutoLogonIfChallenged (more details starting from http://msdn.microsoft.com/en-us/library/aa383153(v=vs.85).aspx). They seem to show this automatically logging on the the proxy server (for HTTP requests), so it may be worth a try.
Thoughts? Hope this helps you out!
Take care,... Russell
Quoted 44 lines · Heiko Hund
Hi,
Quoted 2 lines · On Monday 08 August 2011 01:15:02 Russell Morris wrote:
Has anyone had any luck with the auto-proxy function, in particular with PAC files? It really doesn't seem to be working for me ... :-(.
Are you aware of https://community.openvpn.net/openvpn/ticket/24 ? This is probably what's causing your trouble... and of course the fact that the current impelmentation does not do proxy auto detection.
I'll start resolving these issues now as it is something the GUI is relying onto as well. Openvpn should even be able to handle per Dial-up and VPN connection proxy settings if things work out.
Quoted 4 lines
Also, does anyone have a proxy server that requires authentication? I currently use a text file (which does work!), but there are also Windows functions that should send domain login information automatically. It would be nice to to that ... :-).
I like that idea, but t's probably unrelated to auto-proxy. Could you point me to the APIs.
Heiko
--
Heiko Hund | Software Engineer | Phone +49-721-25516-237 | Fax -200 Astaro a Sophos Company | Amalienbadstr. 41 Bau 52 | 76227 Karlsruhe | Germany Commercial Register: Mannheim HRA 702710 | Headquarter Location: KarlsruheRepresented by the General Partner Astaro Verwaltungs GmbH Amalienbadstraße 41 Bau 52 | 76227 Karlsruhe | Germany Commercial Register: Mannheim HRB 708248 | Executive Board: Gert Hansen, Markus Hennig, Jan Hichert, Günter Junk, Dr. Frank Nellissen
------------------------------------------------------------------------------
BlackBerry® DevCon Americas, Oct. 18-20, San Francisco, CA The must-attend event for mobile developers. Connect with experts. Get tools for creating Super Apps. See the latest technologies. Sessions, hands-on labs, demos & much more. Register early & save!
http://p.sf.net/sfu/rim-blackberry-1
_______________________________________________
Openvpn-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/openvpn-devel